Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hls.js

Package Overview
Dependencies
Maintainers
4
Versions
2945
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hls.js

JavaScript HLS client using MediaSourceExtension

  • 1.5.17
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
580K
decreased by-17.51%
Maintainers
4
Weekly downloads
 
Created

What is hls.js?

hls.js is a JavaScript library that allows you to play HLS (HTTP Live Streaming) streams in browsers that do not support it natively. It works by using Media Source Extensions (MSE) to play HLS streams in browsers like Chrome, Firefox, and Edge.

What are hls.js's main functionalities?

Basic HLS Playback

This code demonstrates how to set up basic HLS playback using hls.js. It checks if HLS is supported, creates an instance of Hls, loads the HLS stream, attaches it to a video element, and starts playback once the manifest is parsed.

const video = document.getElementById('video');
if (Hls.isSupported()) {
  const hls = new Hls();
  hls.loadSource('https://path/to/your/hls/playlist.m3u8');
  hls.attachMedia(video);
  hls.on(Hls.Events.MANIFEST_PARSED, function () {
    video.play();
  });
}

Error Handling

This code demonstrates how to handle errors in hls.js. It listens for the ERROR event and logs the error type and details. If the error is fatal, it attempts to recover based on the type of error.

hls.on(Hls.Events.ERROR, function (event, data) {
  const errorType = data.type;
  const errorDetails = data.details;
  const errorFatal = data.fatal;
  console.error('Error type:', errorType);
  console.error('Error details:', errorDetails);
  if (errorFatal) {
    switch (errorType) {
      case Hls.ErrorTypes.NETWORK_ERROR:
        console.error('Fatal network error encountered, try to recover');
        hls.startLoad();
        break;
      case Hls.ErrorTypes.MEDIA_ERROR:
        console.error('Fatal media error encountered, try to recover');
        hls.recoverMediaError();
        break;
      default:
        hls.destroy();
        break;
    }
  }
});

Quality Level Selection

This code demonstrates how to select a specific quality level for playback. It listens for the MANIFEST_PARSED event, logs the available quality levels, and sets the current level to 720p.

hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
  const availableQualities = hls.levels.map(level => level.height);
  console.log('Available qualities:', availableQualities);
  hls.currentLevel = availableQualities.indexOf(720); // Set to 720p
});

Other packages similar to hls.js

FAQs

Package last updated on 17 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc